package com.bocai.service.impl;

import com.bocai.bean.CashInfo;
import com.bocai.bean.MessageInfo;
import com.bocai.bean.TUserInfo;
import com.bocai.mapper.CashInfoMapper;
import com.bocai.mapper.MessageInfoMapper;
import com.bocai.mapper.TUserInfoMapper;
import com.bocai.service.ICashInfoService;
import com.bocai.vo.CashInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by jyf on 18/4/13.
 */
@Service
public class CashInfoServiceImpl implements ICashInfoService {

    @Autowired
    private CashInfoMapper cashInfoMapper;

    @Autowired
    private TUserInfoMapper userInfoMapper;

    @Autowired
    private MessageInfoMapper messageInfoMapper;

    @Override
    public int insertSelective(CashInfo cashInfo) {
        TUserInfo tUserInfo = userInfoMapper.selectByPrimaryKey(cashInfo.getUserId());
        if (null == tUserInfo) {
            return 0;
        } else {
            if (tUserInfo.getUserStatus() == 1) {
                cashInfo.setCashAccount(tUserInfo.getUserPhone());
                if (tUserInfo.getUserAccount().compareTo(cashInfo.getCashMoney()) >= 0) {
                    tUserInfo.setUserAccount(cashInfo.getCashMoney());
                    tUserInfo.setUserMoneyReward(null);
                    userInfoMapper.updateCashUserMoney(tUserInfo);
                    return cashInfoMapper.insertSelective(cashInfo);
                } else {
                    return 2;
                }
            } else {
                return 3;
            }
        }
    }

    @Override
    public List<CashInfoVo> findAllCashInfo() {
        return cashInfoMapper.selectAllCashInfos();
    }

    @Override
    public int updateCashInfo(Integer cashId, Integer flag) {
        CashInfo cashInfo = cashInfoMapper.selectByPrimaryKey(cashId);
        if (null != cashInfo) {
            if (cashInfo.getCashState() == 1) {
                TUserInfo tUserInfo2 = userInfoMapper.selectByPrimaryKey(cashInfo.getUserId());
                if (null == tUserInfo2) {
                    return 0;
                } else {
                    if (tUserInfo2.getUserStatus() == 1) {
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("cashId", cashId);
                        map.put("flag", flag);
                        map.put("endTime", new Date());
                        int num = cashInfoMapper.updateCashInfo(map);
                        if (num > 0) {
                            MessageInfo messageInfo = new MessageInfo();
                            messageInfo.setUserId(tUserInfo2.getUserId());
                            messageInfo.setCreateTime(new Date());
                            if (flag == 1) {
                                messageInfo.setContent("The money is already on the account. Please pay attention to it.");
                            } else {
                                messageInfo.setContent("If you have refused to present it, please check the balance of the account.");
                            }
                            messageInfo.setTitle("Present information");
                            HashMap<String, Object> map2 = new HashMap<>();
                            map2.put("userId", messageInfo.getUserId());
                            userInfoMapper.updateReadState(map2);
                            messageInfoMapper.insertSelective(messageInfo);
                            switch (flag) {
                                case 1:
                                    // 确定 钱不做任何改变
                                    return 1;
                                case 2:
                                    // 驳回 钱原路返回
                                    TUserInfo tUserInfo = new TUserInfo();
                                    tUserInfo.setUserId(cashInfo.getUserId());
                                    tUserInfo.setUserAccount(cashInfo.getCashMoney());
                                    return userInfoMapper.updateAddCashUserMoney(tUserInfo);
                                default:
                                    return 0;
                            }
                        } else {
                            return 0;
                        }
                    } else {
                        return 2;
                    }
                }
            } else {
                return 0;
            }

        } else {
            return 0;
        }
    }
}
